﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Vit.Web.Validation;
using Vit.Web.Data;

namespace Vit.Web.Services
{
    public class ValidationService : IValidationService
    {
        private readonly IUnityContainer container;

        public ValidationService(IUnityContainer container)
        {
            this.container = container;
        }

        public IValidator<T> GetValidatorFor<T>(T entity)
        {
            try
            {
                return container.Resolve<IValidator<T>>();
            }
            catch
            {
                return null;
            }
        }

        public ValidationState Validate<T>(T entity, DataOperation dataOperation)
        {
            IValidator<T> validator = GetValidatorFor(entity);

            if (validator == null)
                return null;

            return validator.Validate(entity, dataOperation);
        }
    }
}
